﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CQSDK
{
	class UnPack
	{
		byte[] M_bin;
		int location;

		public UnPack()
		{
		}

		public UnPack(byte[] by)
		{
			M_bin = by;
			location = 0;
		}

		public void Empty()
		{
			M_bin = null;
		}

		public byte[] GetAll()
		{
			return M_bin;
		}

		public byte[] GetBin(int len)
		{
			byte[] by = M_bin.Skip(location).Take(len).ToArray();
			location += len;
			return by;
		}

		public byte GetByte()
		{
			byte by = M_bin.ElementAt(location);
			location += 1;
			return by;
		}

		public int GetInt()
		{
			byte[] by = M_bin.Skip(location).Take(4).ToArray();
			by = by.Reverse().ToArray();
			location += 4;
			return BitConverter.ToInt32(by, 0);
		}

		public long GetLong()
		{
			byte[] by = M_bin.Skip(location).Take(8).ToArray();
			by = by.Reverse().ToArray();
			location += 8;
			return BitConverter.ToInt64(by, 0);
		}

		public short GetShort()
		{
			byte[] by = M_bin.Skip(location).Take(2).ToArray();
			by = by.Reverse().ToArray();
			location += 2;
			return BitConverter.ToInt16(by, 0);
		}

		public string GetLenStr()
		{
			int len = GetShort();
			byte[] by = GetBin(len);
			return Encoding.GetEncoding("GB18030").GetString(by);
		}

		public byte[] GetToken()
		{
			int len = GetShort();
			return GetBin(len);
		}

		public int Len()
		{
			return M_bin.Length - location;
		}

		public void SetData(byte[] b)
		{
			M_bin = b;
			location = 0;
		}
	}
}
